package com.example.lvsuo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.lvsuo.Execl.Execldaochu.ExeclService;

import com.example.lvsuo.entity.SystemRole;
import com.example.lvsuo.mapper.SystemRoleMapper;

import com.example.lvsuo.service.ISystemRoleService;

import com.example.lvsuo.entity.vo.SystemRoleVo;
import com.example.lvsuo.service.ISystemRoleService;
import com.example.lvsuo.entity.SystemRole;
import com.example.lvsuo.mapper.SystemRoleMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统角色表，给用户分配的角色 服务实现类
 * </p>
 *
 * @author 心心念念
 * @since 2021-03-30
 */
@Service
public class SystemRoleServiceImpl extends ServiceImpl<SystemRoleMapper, SystemRole> implements ISystemRoleService {
    @Autowired
    ExeclService es;

    @Override
    public void lists(ArrayList<ArrayList<Object>> toString) {
        List<Map<String,String>> ZHONGYAO= new ArrayList<>();
        List<String> header =new ArrayList<>();
        //  改 Task参数为要导入的哪张表的 表名 ----------
        List<Map<String,Object>>  ziduan= es.ziduanANDzhushi("system_role");
        for (int i = 0; i <ziduan.size() ; i++) {
            header.add(ziduan.get(i).get("zds").toString());
        }
        for (int i = 0; i <toString.size() ; i++) {
            HashMap hashMap = new HashMap();
            List     q= (List) toString.get(i);
            for (int j = 0; j <q.size() ; j++) {
                hashMap.put(header.get(j),q.get(j));
            }
            ZHONGYAO.add(hashMap);
        }
        //改方法为mapper的方法---参数固定不可改变---------
        this.baseMapper.selectlistq(ZHONGYAO);
    }


    @Autowired
    SystemRoleMapper systemRoleMapper;


    /**
     * 查询
     *
     * @return
     */
    @Override
    public List<SystemRole> getSystemRole(SystemRole systemRole) {
        return systemRoleMapper.getSystemRole(systemRole);
    }

    /**
     * 添加
     *
     * @param sys
     */
    @Override
    public void insertRoleOrAuthority(SystemRoleVo sys) {
        // 先添加角色
        systemRoleMapper.instRole(sys);
        // 返回新添加的id
        String id = systemRoleMapper.queryInstId();
        sys.setRoleId(id);
        //new一个集合
        List arrayList = new ArrayList();
        //循环要添加的id
        for (int i = 0; i < sys.getList().size(); i++) {
            Map map = new HashMap();
            map.put("authorityId", sys.getList().get(i));
            map.put("roleId", id);
            arrayList.add(map);
        }
        // 添加关联表
        systemRoleMapper.insertRoleOrAuthority(arrayList);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public SystemRoleVo getSystemRoleId(String id) {
       systemRoleMapper.getSystemRoleId(id);

        SystemRole map = systemRoleMapper.getSystemRoleByid(id);//id 查询 角色

       List list = systemRoleMapper.getAuthorityId(id);
        SystemRoleVo systemRoleVo = new SystemRoleVo();
        systemRoleVo.setRoleId(map.getId().toString());
        systemRoleVo.setRoleName(map.getRoleName());
        systemRoleVo.setRoleText(map.getRoleText());
        systemRoleVo.setList(list);


        return systemRoleVo;
    }

    /**
     * 修改
     * @param systemRoleVo
     */
    @Override
    public void updateRoleOrAuthority(SystemRoleVo systemRoleVo) {
        systemRoleMapper.updateRole(systemRoleVo);
        systemRoleMapper.deleteRoleOrAuthority(systemRoleVo.getRoleId());
        //new一个集合
        List arrayList = new ArrayList();
        //循环要添加的id
        for (int i = 0; i < systemRoleVo.getList().size(); i++) {
            Map map = new HashMap();
            map.put("authorityId", systemRoleVo.getList().get(i));
            map.put("roleId", systemRoleVo.getRoleId());
            arrayList.add(map);
        }
        // 添加关联表
        systemRoleMapper.insertRoleOrAuthority(arrayList);
    }

    /**
     * 删除
     * @param
     */
    @Override
    public void deleteRoleOrAuthority(String id) {
        systemRoleMapper.deleteRole(id);
        systemRoleMapper.deleteRoleAndAuthority(id);
    }

    /**
     * 链表查询
     * @return
     */
    @Override
    public List<SystemRole> getSystemRua() {
        return systemRoleMapper.getSystemRua();
    }

    @Override
    public List<SystemRole> queryRole() {
        return systemRoleMapper.queryRole();
    }

}
